from imports import *

class Mario(pygame.sprite.Sprite):
    def __init__(self):
        
        # init sprite
        pygame.sprite.Sprite.__init__(self)

        # images
        self.imagename = imgdir + 'Mariostand.png'
        self.image = image_load(self.imagename)

        # rect
        self.rect = self.image.get_rect()
        self.rect.centerx = 300
        self.rect.bottom = 400

        # blocks
        # should implement a block caching algorithm
        self.blocks = []

        # jumping stuff
        self.onblock = False
        self.jumping = False

        # gravity constants (adjust as needed)
        # gravity parabola = ax^2 + bx + c
        self.a = 0.05
        self.b = 2
        self.c = 0

        # movement constants (speed)
        self.speed = 10
        
        # moving stuff
        self.screenpos = 0
        self.facing = 'R'
        self.xchange = 0
        self.ychange = 0
        self.dy = 0
        
    def gravity(self):
##        if not self.onblock and not self.jumping:
###            print self.a * (self.dy ** 2) + self.b * self.dy + self.c
##            return self.a * (self.dy ** 2) + self.b * self.dy + self.c
###        print self.a * (self.dy ** 2) + self.b * self.dy + self.c
##        return 0
        return self.a * (self.dy ** 2) + self.b * self.dy + self.c

    def update(self):

        # reset xchange and ychange
        self.xchange = 0
        self.ychange = 0

        # get key input
        key = pygame.key.get_pressed()

        # process key input
        if key[K_LEFT]:
            if self.facing == 'R':
                self.facing = 'L'
                self.image = pygame.transform.flip(self.image, 1, 0)
            self.xchange -= self.speed
        if key[K_RIGHT]:
            if self.facing == 'L':
                self.facing = 'R'
                self.image = pygame.transform.flip(self.image, 1, 0)
            self.xchange += self.speed
        if key[K_ESCAPE]:
            pygame.mixer.music.fadeout(3)
            exit()
        if key[K_k]:
            self.reset() # kill button
        if key[K_UP] and not self.jumping:
            self.dy = -15
            self.jumping = True


        
        if not self.onblock:
            self.dy += 1
        self.ychange += self.gravity()

        self.onblock = False
        
        self.rect.right += self.xchange

        self.old_pos = self.rect.centerx
        self.rect.centerx += self.xchange
        self.rect.centery += self.ychange
        
        #print 'self.ychange = ' + str(self.ychange)
        for block in self.blocks:
            if self.rect.colliderect(block.rect):
                top, right, bottom, left, inside = collide_edges(block.rect, self.rect)
                if top and not bottom:
                    self.rect.bottom = block.rect.top
                    self.onblock = True
                    if self.dy >= 0:
                        self.jumping = False
                        self.dy = 0
                        self.ychange = 0
                elif bottom and not top:
                    self.rect.top = block.rect.bottom
                    self.dy = 0
                elif left:
                    self.rect.left = block.rect.right
                elif right:
                    self.rect.right = block.rect.left
                elif inside: # if inside the block
                    print 'inside'
                    if self.xchange > 0:
##                        self.rect.centerx = self.old_pos
                        self.rect.right = block.rect.left
                    elif self.xchange < 0:
##                        self.rect.centerx = self.old_pos
                        self.rect.left = block.rect.right
                    if self.ychange > 0:
                        self.rect.bottom = block.rect.top
                        self.onblock = True
                        self.jumping = False
                        self.dy = 0
                        self.ychange = 0
                    elif self.ychange < 0:
                        self.rect.top = block.rect.bottom



    def reset(self):
        self.screenpos = 0
        self.rect.centerx = 300
        self.rect.bottom = 400
        self.jumptime = 0
        self.jumping = False
        self.dy = 0
        self.xchange = 0
        self.ychange = 0
